<!--
 * @Author: early-autumn
 * @Date: 2020-05-27 13:40:56
 * @LastEditors: early-autumn
 * @LastEditTime: 2020-06-17 15:58:31
-->
<template>
  <el-dialog
    title="修改到店时间"
    width="380px"
    :visible="value"
    @close="handleClose"
  >
    <el-form ref="form" :model="form" :rules="rules" inline>
      <el-form-item label="到店时间" prop="onClinicTime">
        <el-date-picker
          v-model="form.onClinicTime"
          type="datetime"
          placeholder="选择到店时间"
          default-time="12:00:00"
        ></el-date-picker>
      </el-form-item>
    </el-form>

    <div slot="footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" :loading="loading" @click="handleSubmit"
        >确定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import dayjs from 'dayjs'
import { updateAppointment } from '@/api/services/appointment'

export default {
  name: 'ModifyClinicTime',
  model: {
    prop: 'value',
    event: 'close',
  },
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    data: {
      type: Object,
      default: () => ({
        id: 0,
        date: '',
      }),
    },
  },
  data() {
    return {
      loading: false,
      form: {
        onClinicTime: '',
      },
      rules: {
        onClinicTime: {
          required: true,
          message: '请选择到店时间',
          trigger: 'blur',
        },
      },
    }
  },
  watch: {
    value(newVal) {
      if (newVal) {
        this.init()
      } else {
        this.$refs.form.resetFields()
      }
    },
  },
  methods: {
    init() {
      this.form.onClinicTime = this.data.date
    },
    handleClose() {
      this.$emit('close', false)
    },
    handleSubmit() {
      this.$refs.form.validate(async (valid) => {
        try {
          if (!valid) {
            return
          }
          this.loading = true
          const { onClinicTime } = this.form
          await updateAppointment({
            id: this.data.id,
            onClinicTime: dayjs(onClinicTime).format('YYYY-MM-DD HH:mm:ss'),
          })
          this.msgSuccess('修改成功')
          this.handleClose()
          this.$emit('ok')
        } finally {
          this.loading = false
        }
      })
    },
  },
}
</script>

<style></style>
